<template>
  <Card dis-hover :bordered="false">
    <Row class="ivu-mb-16">
      <Col span="22">
        <Input
          placeholder="请输入标签名称"
          v-model="params.keywords"
          @on-clear="searchData"
          @on-enter="searchData"
          clearable
          v-width="150"
        />
        <Select
          placeholder="类型"
          class="ivu-ml-16"
          v-model="params.type"
          clearable
          @on-change="searchData"
          v-width="120"
        >
          <Option v-for="item in typeList" :value="item.id" :key="item.id">{{
            item.name
          }}</Option>
        </Select>
      </Col>
      <Col span="2" style="text-align: right">
        <Button
          type="primary"
          @click="handleAdd"
          v-hasC="{
            name: 'v1-base-tags-create-tags',
            flag: 'v1/base/tags/create-tags',
          }"
          >新增</Button
        >
      </Col>
    </Row>

    <Table
      :columns="columns"
      :data="list"
      @on-sort-change="sortChange"
      :loading="loading"
      :max-height="tableHeigth"
    >
      <template #contenttype="{ row }">
        <span>{{ row.contenttype }}</span>
      </template>

      <template #action="{ row }">
        <a
          @click="handleEdit(row)"
          v-hasC="{
            name: 'v1-base-tags-update-tags',
            flag: 'v1/base/tags/update-tags',
          }"
          >编辑</a
        >
        <Divider
          type="vertical"
          v-hasC="{
            name: 'v1-base-tags-update-tags',
            flag: 'v1/base/tags/update-tags',
          }"
        />
        <a
          @click="handleDel(row.id)"
          class="del"
          v-hasC="{
            name: 'v1-base-tags-delete-tags',
            flag: 'v1/base/tags/delete-tags',
          }"
          >删除</a
        >
      </template>
    </Table>
    <Page
      class="ivu-mt-16"
      :total="total"
      :modelValue="params.current"
      :page-size="params.size"
      :page-size-opts="pageOpts"
      show-total
      show-sizer
      @on-change="changePage"
      @on-page-size-change="changepageSize"
    />
    <Modal
      :mask-closable="false"
      v-model="addModal"
      :title="edit ? '新增' : '编辑'"
      :footer-hide="true"
      :draggable="true"
    >
      <Form ref="create" :model="tagInfo" :rules="rules" :label-width="80">
        <FormItem label="标签名称" prop="name" required>
          <Input v-model="tagInfo.name" placeholder="批量用，分隔" />
        </FormItem>

        <FormItem label="类型" prop="type">
          <Select placeholder="类型" v-model="tagInfo.type">
            <Option v-for="item in typeList" :value="item.id" :key="item.id">{{
              item.name
            }}</Option>
          </Select>
        </FormItem>
      </Form>
      <div slot="footer" style="text-align: right">
        <Button type="default" @click="addModal = false">取消</Button>
        <Button
          type="primary"
          @click="ok"
          style="margin-left: 10px"
          :loading="btnLoading"
          >确定</Button
        >
      </div>
    </Modal>
  </Card>
</template>
<script>
import { tagList, createTag, deleteTag, updateTag } from "@api/tag.js";
export default {
  name: "tagsetup",
  data() {
    return {
      tableHeigth: document.body.clientHeight - 280,
      addModal: false,
      edit: true,
      loading: false,
      btnLoading: false,
      typeList: [],
      params: {
        keywords: "",
        current: 1,
        size: 10,
        type: "",
        order_column: "",
        order_sort: "",
      },
      total: 0,
      pageOpts: [10, 20, 30, 40],
      columns: [
        {
          key: "id",
          title: "ID",
          width: 80,
        },
        {
          key: "name",
          title: "标签名称",
          minWidth: 100,
        },
        {
          key: "type_name",
          title: "类型",
          width: 100,
        },
        {
          key: "operation_name",
          title: "创建人",
          width: 120,
        },
        {
          key: "created_at_str",
          title: "创建日期",
          width: 180,
          sortable: "custom",
        },
        {
          key: "updated_at_str",
          title: "更新日期",
          width: 180,
          sortable: "custom",
        },
        {
          slot: "action",
          title: "操作",
          width: 110,
          fixed: "right",
        },
      ],
      list: [],
      tagInfo: {
        id: "",
        name: "",
        sort: 100,
        status: true,
      },
      rules: {
        name: [{ required: true, message: "请输入标签名称" }],
        type: [{ required: true, message: "请选择类型" }],
      },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    searchData() {
      this.params.current = 1;
      this.getData();
    },
    sortChange(e) {
      this.params.order_column = e.key.replace(/_str$/, "");
      this.params.order_sort = e.order;
      this.getData();
    },
    //切换page
    changePage(index) {
      this.params.current = index;
      this.getData();
    },
    changepageSize(val) {
      this.params.size = val;
      this.getData();
    },
    handleDel(index) {
      this.$Modal.confirm({
        title: "删除提示",
        content: "是否确认删除此条记录，删除后不可恢复",
        onOk: () => {
          deleteTag({ id: index })
            .then(() => {
              this.$Message.success("删除成功");
              this.getData();
            })
            .catch(() => {});
        },
      });
    },
    handleAdd() {
      this.tagInfo = {
        id: "",
        name: "",
        sort: 100,
        status: true,
      };
      this.addModal = true;
      this.edit = true;
      this.$refs.create.resetFields();
    },
    handleEdit(info) {
      this.addModal = true;
      this.edit = false;
      this.tagInfo = {
        id: info.id,
        name: info.name,
        type: parseInt(info.type),
      };
    },
    getData() {
      this.loading = true;
      const params = {
        pageSize: this.params.size,
        page: this.params.current,
        keywords: this.params.keywords,
        order_column: this.params.order_column,
        order_sort: this.params.order_sort,
        type: this.params.type,
      };
      tagList(params)
        .then((res) => {
          this.list = res.list;
          this.total = res.total;
          this.typeList = res.type_list;
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    addTag() {
      this.btnLoading = true;
      const params = {
        name: this.tagInfo.name,
        type: this.tagInfo.type,
      };
      if (this.edit) {
        createTag(params)
          .then((res) => {
            this.$Message.success("添加成功");
            this.addModal = false;
            this.btnLoading = false;
            this.params.current = 1;
            this.getData();
          })
          .catch(() => {
            this.btnLoading = false;
          });
      } else {
        updateTag(Object.assign({}, params, { id: this.tagInfo.id }))
          .then((res) => {
            this.$Message.success("编辑成功");
            this.addModal = false;
            this.btnLoading = false;
            this.getData();
          })
          .catch(() => {
            this.btnLoading = false;
          });
      }
    },
    ok() {
      this.$refs.create.validate((valid) => {
        if (valid) {
          this.addTag();
        } else {
          this.addModal = true;
          // this.$Message.error('填写错误，保存失败！')
          return;
        }
      });
    },
  },
};
</script>
